﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using System.Xml;
using NCKH.models;

namespace NCKH.Test
{
    public class Functions
    {


        public static void Save(string path)
        {
            XmlDocument xmlDocOutput = new XmlDocument();
            xmlDocOutput.Load(path);

            XmlNode xmlElementOutput = xmlDocOutput.DocumentElement;
            XmlNode xmlNodeOutput = xmlElementOutput.SelectSingleNode("descendant::Activities_Timetable");

            var ketQuas = new List<Ketqua>();
            for (int i = 0; i < xmlElementOutput.ChildNodes.Count; i++)
            {
                XmlNode xmlNode = xmlElementOutput.ChildNodes.Item(i);

                int id = int.Parse(xmlNode.ChildNodes.Item(0).InnerText.Trim());

                string day = xmlNode.ChildNodes.Item(1).InnerText.Trim();
                if (day == "") continue;
                int n1 = Methods.Days.IndexOf(day);
                string hour = xmlNode.ChildNodes.Item(2).InnerText.Trim();
              
                int n2 = Methods.Times.IndexOf(hour);

                var tmp3 = xmlNode.ChildNodes.Item(3).InnerText.Trim();

                if (tmp3 == string.Empty) continue;
                int n3 = int.Parse(tmp3);

                //var rangBuoc = activities.Where(w => w.Sukien.ID == id).Select(s => s.RangBuoc).ToList().FirstOrDefault();

                //int ID_cb = rangBuoc == null ? 0 : (rangBuoc.DS_giang_vien == "" || rangBuoc.DS_giang_vien == null) ? 0 : int.Parse(rangBuoc.DS_giang_vien.Split(',')[0]);

                ketQuas.Add(new Ketqua
                {
                    ID = id,
                    ID_cb = 0,
                    ID_phong = n3,
                    Thu = n1,
                    Ca_hoc = (n2 > 1) ? n2 : n2 + 1,
                });
            }

            //Ketquas

            Context db = new Context();
            //var IDs = activities.Select(s => s.Sukien.ID).Distinct().ToList();

            var details = db.EXM_SuKienPhongThis.ToList();
            int count = 0;
            foreach (var detail in details)
            {
                var ketQua = ketQuas.Where(w => w.ID == detail.ID).ToList().FirstOrDefault();
                if (ketQua == null) continue;
                detail.ID_phong = ketQua.ID_phong;
                detail.ID_cb = ketQua.ID_cb;
                detail.Thu = ketQua.Thu;
                detail.So_tiet_thi = 2;
                detail.Ca_hoc = ketQua.Ca_hoc;
                detail.Da_xep_lich = true;
                count++;
            }
            db.SubmitChanges();
        }

        //    public static void Repair()
        //    {
        //        DataContext db = new DataContext();

        //        var suKiens = db.PLAN_SukiensTinChi_TCs.Where(w => w.Ky_dang_ky == Program.DataSchedule.Ky_dang_ky && w.Da_xep_lich == true).ToList();

        //        var thus = suKiens.Select(s => s.Thu).Distinct().ToList();
        //        var tiets = suKiens.Select(s => s.Tiet).Distinct().ToList();
        //        suKiens = (from p in suKiens orderby p.Thu, p.Tiet select p).ToList();
        //        using (TransactionScope scope = new TransactionScope())
        //        {
        //            for (int i = 0; i < suKiens.Count; i++)
        //            {
        //                if (((suKiens[i].Tiet == 1 || suKiens[i].Tiet == 6) && suKiens[i].Thu == 0) || (suKiens[i].Thu != 0 && (suKiens[i].Tiet == 0 || suKiens[i].Tiet == 5))) continue;

        //                if (((suKiens[i].Tiet == 2 || suKiens[i].Tiet == 7) && suKiens[i].Thu == 0) || (suKiens[i].Thu != 0 && (suKiens[i].Tiet == 1 || suKiens[i].Tiet == 6)))
        //                {
        //                    var entity = db.PLAN_SukiensTinChi_TCs.Where(w => w.ID == suKiens[i].ID).ToList();
        //                    entity.ForEach(f => { f.Tiet = suKiens[i].Tiet - 1; });
        //                    db.SubmitChanges();
        //                }
        //                else
        //                {
        //                    var tmp2 = suKiens.Where(w => w.Thu == suKiens[i].Thu && w.Tiet == suKiens[i].Tiet - 2 && w.ID_phong == suKiens[i].ID_phong).ToList();
        //                    if (tmp2 != null)
        //                    {
        //                        var tmp1 = suKiens.Where(w => w.Thu == suKiens[i].Thu && w.Tiet == suKiens[i].Tiet - 1 && w.ID_phong == suKiens[i].ID_phong).ToList();
        //                        if (tmp1 == null)
        //                        {
        //                            var entity = db.PLAN_SukiensTinChi_TCs.Where(w => w.ID == suKiens[i].ID).ToList();
        //                            entity.ForEach(f => { f.Tiet = suKiens[i].Tiet - 1; });
        //                            db.SubmitChanges();
        //                        }
        //                    }
        //                }
        //            }
        //            scope.Complete();
        //        }
        //    }
        //    public static void RepairX2()
        //    {
        //        DataContext db = new DataContext();

        //        var suKiens = db.PLAN_SukiensTinChi_TCs.Where(w => w.Ky_dang_ky == Program.DataSchedule.Ky_dang_ky && w.Da_xep_lich == true).ToList();

        //        var thus = suKiens.Select(s => s.Thu).Distinct().ToList();
        //        var tiets = suKiens.Select(s => s.Tiet).Distinct().ToList();
        //        suKiens = (from p in suKiens orderby p.Thu, p.Tiet select p).ToList();
        //        using (TransactionScope scope = new TransactionScope())
        //        {
        //            for (int i = 0; i < suKiens.Count; i++)
        //            {
        //                if (((suKiens[i].Tiet == 2 || suKiens[i].Tiet == 7) && suKiens[i].Thu == 0) || (suKiens[i].Thu != 0 && (suKiens[i].Tiet == 1 || suKiens[i].Tiet == 6)))
        //                {
        //                    var entity = db.PLAN_SukiensTinChi_TCs.Where(w => w.ID == suKiens[i].ID).ToList();
        //                    entity.ForEach(f => { f.Tiet = suKiens[i].Tiet - 2; });
        //                    db.SubmitChanges();
        //                }
        //                else
        //                {
        //                    var tmp2 = suKiens.Where(w => w.Thu == suKiens[i].Thu && w.Tiet == suKiens[i].Tiet - 2 && w.ID_phong == suKiens[i].ID_phong).ToList();
        //                    if (tmp2 != null)
        //                    {
        //                        var tmp1 = suKiens.Where(w => w.Thu == suKiens[i].Thu && w.Tiet == suKiens[i].Tiet - 1 && w.ID_phong == suKiens[i].ID_phong).ToList();
        //                        if (tmp1 == null)
        //                        {
        //                            var entity = db.PLAN_SukiensTinChi_TCs.Where(w => w.ID == suKiens[i].ID).ToList();
        //                            entity.ForEach(f => { f.Tiet = suKiens[i].Tiet - 2; });
        //                            db.SubmitChanges();
        //                        }
        //                    }
        //                }
        //            }
        //            scope.Complete();
        //        }
        //    }

        //    public static void Synchronous(List<SuKiensTC> activities)
        //    {
        //        DataContext db = new DataContext();

        //        var suKiensTC = db.PLAN_SukiensTinChi_TCs.Where(w => w.Ky_dang_ky == Program.DataSchedule.Ky_dang_ky).ToList();

        //        for (int i = 0; i < activities.Count; )
        //        {
        //            var tmp1 = suKiensTC.Where(w => w.ID_lop_tc == activities[i].Sukien.ID_lop_tc).ToList();

        //            var tmp2 = suKiensTC.Where(w => w.ID_lop_tc == activities[i].Sukien.ID_lop_tc && w.Tu_ngay_dk > activities[i].Sukien.Tu_ngay_dk && w.Den_ngay_dk > activities[i].Sukien.Den_ngay_dk).ToList();

        //            for (int j = 0; j < tmp1.Count; j++)
        //            {
        //                if (j >= tmp2.Count) break;
        //                if (tmp1[j].Da_xep_lich == false) continue;

        //                tmp2[j].ID_phong = tmp1[j].ID_phong;
        //                tmp2[j].ID_cb = tmp1[j].ID_cb;
        //                tmp2[j].Thu = tmp1[j].Thu;
        //                tmp2[j].Tiet = tmp1[j].Tiet;
        //            }
        //            i += tmp1.Count;
        //        }
        //    }
        //}

        public partial class Ketqua
        {
            public int ID { get; set; }
            public int ID_phong { get; set; }
            public int Thu { get; set; }
            public int Ca_hoc { get; set; }
            public int ID_cb { get; set; }
        }
    }
}
